package com.peak.distribution.manager.order.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;
import com.peak.distribution.entity.order.DisOrder;
import com.peak.distribution.enums.OrderStatus;
import com.peak.distribution.manager.order.DisOrderManager;
import com.peak.distribution.repository.order.DisOrderDao;
import com.peak.spring.boot.builder.repository.FilterParamBuilder;
import com.peak.spring.boot.core.manager.BaseManagerImpl;
import com.peak.spring.boot.tools.Converts;

@Component
@Transactional(readOnly = true)
public class DisOrderManagerImpl extends BaseManagerImpl<DisOrder> implements DisOrderManager {

	@Autowired
	private DisOrderDao orderDao;
	
	@Override
	public Map<String, Integer> countOrderByStatus(Integer agentId) {
		List<Object[]> list = orderDao.countOrderByStatus(agentId);
		Map<String, Integer> result = Maps.newHashMap();
		list.stream().forEach(p -> result.put(((OrderStatus)p[1]).name(), Converts.toInteger(p[0])));
		return result;
	}

	@Override
	public Map<String, Integer> countOrderByStatus(OrderStatus... orderStatuses) {
		Map<String, Integer> map = Maps.newLinkedHashMap();
		for (OrderStatus orderStatus : orderStatuses) {
			int count = super.count(FilterParamBuilder.create().eq("status", orderStatus).build());
			map.put(orderStatus.name(), count);
		}
		return map;
	}

}
